package com.wjjumpad.cwjjumpad.base;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.content.ContextCompat;
import androidx.appcompat.widget.Toolbar;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
//import android.widget.Toolbar;

import com.wjjumpad.cwjjumpad.R;


/**
 * 提供Activity的基类封装
 * Basic Activity class for inheritance of all activity
 */

public abstract class QLBaseActivity extends AppCompatActivity implements QLBaseIView {

    private Toolbar toolbar;
    protected ImageView action_bar_back,action_bar_right;
    protected TextView actionBarTitle,actionBarRightBtn;
    private View actionBarLine;
    private CoordinatorLayout rootLayout;
    private View mSuccessView;

    private FrameLayout mCommonContainer;

//    //用于管理Activity中使用RxJava取消订阅
//    protected final CompositeDisposable lifecycleDisposable = new CompositeDisposable();
    /**
     * 页面根视图对象
     */
    // protected ViewGroup rootView;
    // private Dialog progressDlg;

    // @Override
    // protected void attachBaseContext(Context newBase) {
    //     super.attachBaseContext(FContextWrapper.wrap(newBase, LanguageUtils.getBaseLocale()));
    //     Configuration config = new Configuration();
    //     applyOverrideConfiguration(config);
    // }

    // @Override
    // public void applyOverrideConfiguration(Configuration overrideConfiguration) {
    //     if (overrideConfiguration != null) {
    //         int uiMode = overrideConfiguration.uiMode;
    //         overrideConfiguration.setTo(FApp.getResources().getConfiguration());
    //         overrideConfiguration.uiMode = uiMode;
    //     }
    //     super.applyOverrideConfiguration(overrideConfiguration);
    // }

    public abstract int getLayout();

    public abstract void init();

    /**
     * 通过模板模式 来控制子类的模板函数调用
     */
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWhiteWindows();
        setContentView(R.layout.activity_base);
//        initCommonView();
        //设置竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    }

    public void start(Class c){
        Intent intent = new Intent(this,c);
        startActivity(intent);
    }

    //    初始化主页面
    private void initCommonView() {
        // MyActivityManager.getInstance().addActivityStack(initActivity());
        //初始化界面
        initView();
        // initActivityData();
    }
    private void initView() {
        rootLayout = (CoordinatorLayout) findViewById(R.id.rootLayout);
        mCommonContainer = (FrameLayout) findViewById(R.id.home_common_container);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        action_bar_back = (ImageView) findViewById(R.id.action_bar_back);
        action_bar_right = (ImageView) findViewById(R.id.action_bar_right);
        actionBarTitle = (TextView) findViewById(R.id.action_bar_title);
        actionBarRightBtn = (TextView) findViewById(R.id.action_bar_right_btn);
        actionBarLine = findViewById(R.id.action_bar_line);

        Log.d("QLBaseActivity", "initView ");

       if (mSuccessView == null) {
           mSuccessView = initSuccessView();
           if (mSuccessView!=null) {
               mCommonContainer.addView(mSuccessView);
               mSuccessView.setVisibility(View.VISIBLE);
           }
       }

    }

    /**
     * 成功页面
     */
    public abstract View initSuccessView();

    /**
     * <--------------------------------------actionbar管理-------------------------------------------------->
     */


    private void setOnclick() {
        action_bar_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                actionbarDoBackOnclick();
            }
        });
        action_bar_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                actionbarRightImgOnclick();
            }
        });
        actionBarRightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                actionbarDoRightOnclick();
            }
        });

    }

    /**
     * actionbar右边按钮点击事件
     */
    public void actionbarDoRightOnclick() {

    }

    /**
     * actionbar左边按钮点击事件
     */
    public void actionbarDoBackOnclick() {



    }
    public void actionbarRightImgOnclick() {

    }

     public void hideBack(){
        action_bar_back.setVisibility(View.GONE);
    }
    public void showRightImg(){
        action_bar_right.setVisibility(View.VISIBLE);
    }

    public void hideActionBarLine(){
        actionBarLine.setVisibility(View.GONE);
    }
    public void showActionBarLine(){
        actionBarLine.setVisibility(View.VISIBLE);
    }

     //设置标题
    public void setBackIcon(int drawableId){
        showActionBar();
        // action_bar_back.setImageResource(drawableId);
        // actionBarTitle.setVisibility(View.VISIBLE);

    }

     @Override
    public void showActionBar(){
        mCommonContainer.setPadding(0, (int) getResources().getDimension(R.dimen.dp_43),0,0);
         rootLayout.setFitsSystemWindows(true);
         toolbar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideActionBar(){
         rootLayout.setFitsSystemWindows(false);
        mCommonContainer.setPadding(0, 0,0,0);
         toolbar.setVisibility(View.GONE);
    }

    //设置标题
    @Override
    public void setTitle(String title){
        showActionBar();
         actionBarTitle.setText(title);
         actionBarTitle.setTextColor(getResources().getColor(R.color.actionBarTitleColor));
         toolbar.setBackgroundColor(getResources().getColor(R.color.actionBarBackgroundColor));
         actionBarTitle.setVisibility(View.VISIBLE);

    }

    @Override
    public void setActionTitle(String title){
        initCommonView();
        toolbar.setVisibility(View.VISIBLE);
        actionBarTitle.setText(title);
        actionBarTitle.setVisibility(View.VISIBLE);
        Log.d("QLBaseActivity", "setActionTitle -  title: "+title);
        setOnclick();

    }

//    public void setOnClick(Button btn,View.OnClickListener listener){
        // btn.setOnClickListener(new View.OnClickListener() {
        //     @Override
        //     public void onClick(View v) {

        //     }
        // });
//        btn,setOnClickListener(listener);
//    }

    /**
     * 得到自定义的progressDialog
     *
     * @param msg
     * @return
     */
    // public Dialog createLoadingDialog(String msg) {
    //     LayoutInflater inflater = LayoutInflater.from(this);
    //     View v = inflater.inflate(R.layout.view_loading_dialog, null);// 得到加载view
    //     LinearLayout layout = v.findViewById(R.id.dialog_view);// 加载布局
    //     // main.xml中的ImageView
    //     TextView tipTextView = v.findViewById(R.id.circle_text);// 提示文字
    //     tipTextView.setText(msg);// 设置加载信息
    //     Dialog loadingDialog = new Dialog(this, R.style.loading_dialog);// 创建自定义样式dialog
    //     loadingDialog.setOnKeyListener((DialogInterface dialog, int keyCode, KeyEvent event) -> (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0));
    //     loadingDialog.setCanceledOnTouchOutside(false);
    //     loadingDialog.setCancelable(false);// 不可以用“返回键”取消
    //     loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
    //             LinearLayout.LayoutParams.MATCH_PARENT,
    //             LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
    //     return loadingDialog;

    // }

    /**
     * <--------------------------------------状态栏管理-------------------------------------------------->
     */

    /**
     * 设置沉浸式窗口
     */
    private View blueView;
    public void initWindows() {
        Window window = getWindow();
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {//5.1
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //设置状态栏颜色
            window.setStatusBarColor(ContextCompat.getColor(this,R.color.cyan));
            //   window.setStatusBarColor(Color.TRANSPARENT);
            //设置导航栏颜色
           window.setNavigationBarColor(ContextCompat.getColor(this,R.color.cyan));

//            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//设置状态栏黑色字体
            ViewGroup contentView = ((ViewGroup) findViewById(android.R.id.content));

            View childAt = contentView.getChildAt(0);
            if (childAt != null) {
                childAt.setFitsSystemWindows(true);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            //设置contentview为fitsSystemWindows
            ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content);
            View childAt = contentView.getChildAt(0);
            if (childAt != null) {
                childAt.setFitsSystemWindows(true);
            }
            //给statusbar着色
            if(blueView !=null){
                contentView.removeView(blueView);
            }
            if(blueView==null)
                blueView = new View(this);
            blueView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(this)));
            blueView.setBackgroundColor(Color.TRANSPARENT);
            blueView.setBackgroundColor(ContextCompat.getColor(this,R.color.cyan));

//            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//设置状态栏黑色字体
            contentView.addView(blueView);
        }
    }

    /**
     * 设置沉浸式窗口 ,白色标题栏
     */
    private View whiteView;
    public void initWhiteWindows() {
        Window window = getWindow();
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {//5.1

            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            // 禁止全屏模式下内容区域被系统窗口挤压
//            window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
//                    | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
            //设置状态栏颜色
//            window.setStatusBarColor(Color.WHITE);
            window.setStatusBarColor(ContextCompat.getColor(this,R.color.dark_cyan));

//            window.setNavigationBarColor(Color.TRANSPARENT);
            //设置导航栏颜色
            window.setNavigationBarColor(ContextCompat.getColor(this,R.color.dark_cyan));

//            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//设置状态栏黑色字体
            getWindow().getDecorView().setSystemUiVisibility(1);//设置状态栏
            ViewGroup contentView = ((ViewGroup) findViewById(android.R.id.content));

            View childAt = contentView.getChildAt(0);
            if (childAt != null) {
                childAt.setFitsSystemWindows(true);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.setNavigationBarColor(ContextCompat.getColor(this,R.color.cyan));
            //设置contentview为fitsSystemWindows
            ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content);
            View childAt = contentView.getChildAt(0);
            if (childAt != null) {
                childAt.setFitsSystemWindows(true);
            }
            //给statusbar着色
            if(whiteView!=null)
                contentView.removeView(whiteView);
            if(whiteView==null)
                whiteView = new View(this);
            whiteView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(this)));
            //   view.setBackgroundColor(Color.TRANSPARENT);
            whiteView.setBackgroundColor(Color.WHITE);

            // getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//设置状态栏黑色字体
            getWindow().getDecorView().setSystemUiVisibility(1);//设置状态栏
            contentView.addView(whiteView);
        }
    }





    /**
     * 获取状态栏高度
     *
     * @param context context
     * @return 状态栏高度
     */
    private static int getStatusBarHeight(Context context) {
        // 获得状态栏高度
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        return context.getResources().getDimensionPixelSize(resourceId);
    }

}
